package voxeltest;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;
import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.GLU;
import org.lwjgl.util.vector.Matrix4f;

public class Game {

	final static int width = 800, height = 600;
	final static int frameRate = 90;
	boolean[] keys = new boolean[256];
	Camera camera;
	
	
	public static void main(String[] args) throws LWJGLException {
		
		Display.setDisplayMode(new DisplayMode(width,height));
		Display.create();
		Game game = new Game();
		game.init3D();
		
		Chunk ck = new Chunk();
		
		
		long time = System.currentTimeMillis();
		int FPS = 0;
		while(!Display.isCloseRequested()){
			
			game.update();
			
			ck.update();
			
			game.render();
			ck.redraw();
			
			Display.update();
			Display.sync(frameRate);
			
			if(System.currentTimeMillis() - time < 1000){
				FPS++;
			}
			else{
				Display.setTitle("FPS "+ FPS);
				FPS = 0;
				time = System.currentTimeMillis();
			}
		}
		Display.destroy();
		System.exit(0);
	}
	
	public Game(){
		camera = new Camera(this);
	}
	
	public void render(){
		clearScreen();
		camera.translatePostion();
		GL11.glBegin(GL11.GL_LINES);
			//x
			GL11.glColor3f(1,0,0);
			GL11.glVertex3f(0,1,0);
			GL11.glVertex3f(2,1,0);
			
			//y
			GL11.glColor3f(0,1,0);
			GL11.glVertex3f(0,1,0);
			GL11.glVertex3f(0,3,0);			
			
			//z
			GL11.glColor3f(0,0,1);
			GL11.glVertex3f(0,1,0);
			GL11.glVertex3f(0,1,2);
		GL11.glEnd();
	}
	public void update(){
		mapKeys();
		camera.update();
	}
	
	private void mapKeys(){
		//Update keys
		for(int i=0;i<keys.length;i++){
			keys[i] = Keyboard.isKeyDown(i);
		}
	}
	
	
    public static FloatBuffer asFloatBuffer(Matrix4f matrix4f) {
        FloatBuffer buffer = BufferUtils.createFloatBuffer(16);
        matrix4f.store(buffer);
        return buffer;
    }
    
    public FloatBuffer asFlippedFloatBuffer(Matrix4f matrix4f) {
        FloatBuffer buffer = BufferUtils.createFloatBuffer(16);
        matrix4f.store(buffer);
        buffer.flip();
        return buffer;
    }
	
	public void init3D(){
		//Start 3D Stuff
		GL11.glMatrixMode(GL11.GL_PROJECTION);
		GL11.glLoadIdentity();

		GLU.gluPerspective((float) 80, width / height, 0.001f, 1000);
		GL11.glMatrixMode(GL11.GL_MODELVIEW);

		//GL11.glEnable(GL11.GL_TEXTURE_2D);
		GL11.glShadeModel(GL11.GL_SMOOTH);
		GL11.glClearColor(0.2f, 0.2f, 0.2f, 0.0f);
		GL11.glClearDepth(1.0f);
		GL11.glEnable(GL11.GL_DEPTH_TEST);
		GL11.glDepthFunc(GL11.GL_LEQUAL);
		
		glPolygonMode(GL11.GL_FRONT_AND_BACK, GL_LINE);
		
		GL11.glFrontFace(GL11.GL_CW);
        GL11.glCullFace(GL11.GL_BACK);
		
		
        //GL11.glEnable(GL11.GL_COLOR_MATERIAL);
        //GL11.glColorMaterial(GL11.GL_FRONT_AND_BACK, GL11.GL_AMBIENT_AND_DIFFUSE); 
        
    	//GL11.glEnable(GL_BLEND);
    	//GL11.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        
        //GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
        //GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
	}
	
	public void clearScreen(){
		//Clear the screen
		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
		GL11.glLoadIdentity();
	}
}
